// define INPUT  1 // is really 0 for control register!
// define OUTPUT 0 // is really 1 for control register!
// define ALT0   4
//
// define HIGH 1
// define LOW  0
//
// define PUD_OFF  0
// define PUD_DOWN 1
// define PUD_UP   2


var PINIO_MODE_ARRAY = [
    ['BOARD', '0'],
    ['BCM', '1']
];

var PINIO_PIN_LEVEL_ARRAY = [
    ['低电平', '0'],
    ['高电平', '1']
];

var PINIO_PIN_MODE_ARRAY = [
    ['输入', '0'],
    ['输出', '1']
];

var PINIO_PIN_RESISTOR_ARRAY = [
    ['上拉电阻', '0'],
    ['下拉电阻', '1']
];

var PINIO_PIN_EVENT_ARRAY = [
    ['上升沿', 'GPIO.RISING'],
    ['下降沿', 'GPIO.FALLING'],
    ['全部', 'GPIO.BOTH'],
];


Blockly.Blocks['pinio_package'] = {
    init: function() {
        this.appendDummyInput()
            .appendField('导入 RPi.GPIO 包为 GPIO');
        this.setOutput(false);
        this.setColour('%{BKY_RASPBERRY_PINIO_HUE}');
        this.setNextStatement(true);
        this.setPreviousStatement(true);
    }
};

Blockly.Blocks['pinio_mode'] = {
    init: function() {
        this.appendDummyInput()
            .appendField('引脚编码模式 ')
            .appendField(new Blockly.FieldDropdown(PINIO_MODE_ARRAY), 'GPIO_MODE');
        this.setOutput(true, 'Number');
        this.setColour('%{BKY_RASPBERRY_PINIO_HUE}');
        this.setTooltip('引脚编码模式，GPIO.BCM或GPIO.BOARD');
        this.setHelpUrl('');
    }
};

Blockly.Blocks['pinio_get_mode'] = {
    init: function() {
        this.appendDummyInput()
            .appendField('获取 引脚编码模式')
        this.setOutput(true, 'Number');
        this.setColour('%{BKY_RASPBERRY_PINIO_HUE}');
        this.setTooltip('获取树莓派当前的引脚编码模式');
        this.setHelpUrl('');
    }
};

Blockly.Blocks['pinio_set_mode'] = {
    init: function() {
        this.appendDummyInput()
            .appendField('设置 引脚编码模式为 ')
            .appendField(new Blockly.FieldDropdown(PINIO_MODE_ARRAY), 'GPIO_MODE');
        this.setOutput(false);
        this.setColour('%{BKY_RASPBERRY_PINIO_HUE}');
        this.setNextStatement(true);
        this.setPreviousStatement(true);
        this.setTooltip('设置树莓派当前的引脚编码模式');
        this.setHelpUrl('');
    }
};

// Blockly.Blocks['pinio_pin_number'] = {
//     init: function() {
//         this.appendDummyInput()
//             .appendField('引脚 ')
//             .appendField(new Blockly.FieldNumber(13), 'PIN_NUMBER');
//         this.setOutput(true, 'Number');
//         this.setColour('%{BKY_RASPBERRY_PINIO_HUE}');
//         this.setTooltip('BOARD或者BCM引脚标号');
//         this.setHelpUrl('');
//     }
// };

Blockly.Blocks['pinio_pin_mode'] = {
    init: function() {
        this.appendDummyInput()
            .appendField('引脚模式 ')
            .appendField(new Blockly.FieldDropdown(PINIO_PIN_MODE_ARRAY), 'PIN_MODE');
        this.setOutput(true, 'Number');
        this.setColour('%{BKY_RASPBERRY_PINIO_HUE}');
        this.setTooltip('引脚模式，GPIO.IN或GPIO.OUT');
        this.setHelpUrl('');
    }
};

Blockly.Blocks['pinio_set_pin_mode'] = {
    init: function() {
        this.appendValueInput('PIN_NUMBER')
            .appendField('设置 引脚 ')
            .setCheck('Number');
        this.appendDummyInput()
            .appendField(' 模式为 ')
            .appendField(new Blockly.FieldDropdown(PINIO_PIN_MODE_ARRAY, function (option) {
                var resistor = (option==0);
                this.getSourceBlock().updateShape_(resistor);
            }), 'PIN_MODE');
        this.appendDummyInput('RESISTOR')
            .appendField(' ，内部 ')
            .appendField(new Blockly.FieldDropdown(PINIO_PIN_RESISTOR_ARRAY), 'PIN_RESISTOR');
        this.setOutput(false);
        this.setNextStatement(true);
        this.setPreviousStatement(true);
        // this.setMutator(new Blockly.Mutator(['pinio_set_pin_mode_mutator']));
        this.setInputsInline(true);
        this.setColour('%{BKY_RASPBERRY_PINIO_HUE}');
        this.setTooltip('设置引脚模式，GPIO.OUTPUT或GPIO.INPUT');
        this.setHelpUrl('');
    },
    mutationToDom: function () {
        var container = Blockly.utils.xml.createElement('mutation');
        var resistor = this.getFieldValue('PIN_MODE')==0;
        container.setAttribute('resistor', resistor);

        return container;
    },
    domToMutation: function (xmlElement) {
        var resistor = xmlElement.getAttribute('resistor');
        this.updateShape_(resistor);
    },
    updateShape_: function (resistor) {
        if(resistor){
            if(this.getInput('LEVEL')){
                this.removeInput('LEVEL');
            }
            if(this.getInput('RESISTOR')){
                this.removeInput('RESISTOR');
            }

            this.appendDummyInput('RESISTOR')
                .appendField(' ，内部 ')
                .appendField(new Blockly.FieldDropdown(PINIO_PIN_RESISTOR_ARRAY), 'PIN_RESISTOR');
        }else{
            if(this.getInput('LEVEL')){
                this.removeInput('LEVEL');
            }
            if(this.getInput('RESISTOR')){
                this.removeInput('RESISTOR');
            }

            this.appendDummyInput('LEVEL')
                .appendField(' ，初始为 ')
                .appendField(new Blockly.FieldDropdown(PINIO_PIN_LEVEL_ARRAY), 'INIT_LEVEL');
        }
    }
};

Blockly.Blocks['pinio_pin_level'] = {
    init: function() {
        this.appendDummyInput()
            .appendField('引脚电平 ')
            .appendField(new Blockly.FieldDropdown(PINIO_PIN_LEVEL_ARRAY), 'PIN_LEVEL');
        this.setOutput(true, 'Number');
        this.setColour('%{BKY_RASPBERRY_PINIO_HUE}');
        this.setTooltip('引脚电平，GPIO.HIGH或GPIO.LOW');
        this.setHelpUrl('');
    }
};

Blockly.Blocks['pinio_set_pin_level'] = {
    init: function() {
        this.appendValueInput('PIN_NUMBER')
            .appendField('设置 引脚 ')
            .setCheck('Number');
        this.appendDummyInput()
            .appendField(' 电平为 ')
            .appendField(new Blockly.FieldDropdown(PINIO_PIN_LEVEL_ARRAY), 'PIN_LEVEL');
        this.setOutput(false);
        this.setNextStatement(true);
        this.setPreviousStatement(true);
        this.setColour('%{BKY_RASPBERRY_PINIO_HUE}');
        this.setTooltip('设置引脚电平，GPIO.HIGH或GPIO.LOW');
        this.setHelpUrl('');
    }
};

Blockly.Blocks['pinio_get_pin_level'] = {
    init: function() {
        this.appendValueInput('PIN_NUMBER')
            .setCheck('Number')
            .appendField('获取引脚 ');
        this.appendDummyInput()
            .appendField(' 电平');
        this.setOutput(true, 'Number');
        this.setColour('%{BKY_RASPBERRY_PINIO_HUE}');
        this.setTooltip('获取引脚电平，GPIO.HIGH或GPIO.LOW');
        this.setHelpUrl('');
    }
};

Blockly.Blocks['pinio_pin_event'] = {
    init: function() {
        this.appendDummyInput()
            .appendField(new Blockly.FieldDropdown(PINIO_PIN_EVENT_ARRAY), 'PIN_EVENT');
        this.setOutput(true, 'PIN_EVENT');
        this.setColour('%{BKY_RASPBERRY_PINIO_HUE}');
        this.setTooltip('引脚电平变化');
        this.setHelpUrl('');
    }
};

Blockly.Blocks['pinio_add_event_detect'] = {
    init: function() {
        this.appendValueInput('PIN_NUMBER')
            .setCheck('Number')
            .appendField('添加检测 引脚 ');
        this.appendValueInput('PIN_EVENT')
            .setCheck('PIN_EVENT')
            .appendField('事件 ')
            .setAlign(Blockly.ALIGN_RIGHT);
        this.appendValueInput('CALLBACK')
            .setCheck('CALLBACK')
            .appendField('回调 ')
            .setAlign(Blockly.ALIGN_RIGHT);
        this.appendValueInput('BOUNCE')
            .setCheck('Number')
            .appendField('抖动时间 ')
            .setAlign(Blockly.ALIGN_RIGHT);
        this.setOutput(false);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour('%{BKY_RASPBERRY_PINIO_HUE}');
        this.setTooltip('设置监听引脚事件');
        this.setHelpUrl('');
    }
};

Blockly.Blocks['pinio_remove_event_detect'] = {
    init: function() {
        this.appendValueInput('PIN_NUMBER')
            .setCheck('Number')
            .appendField('移除检测 引脚 ');
        this.setOutput(false);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour('%{BKY_RASPBERRY_PINIO_HUE}');
        this.setTooltip('移除监听引脚事件');
        this.setHelpUrl('');
    }
};

Blockly.Blocks['pinio_cleanup'] = {
    init: function() {
        this.appendDummyInput()
            .appendField('引脚复位');
        this.setOutput(false);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setColour('%{BKY_RASPBERRY_PINIO_HUE}');
        this.setTooltip('引脚复位');
        this.setHelpUrl('');
    }
};